﻿using System;
using System.Data;
using org.se.fs.presenter.Library;

namespace org.se.fs.presenter.View.Profile
{
    public class ClientRecords : Template.SubMenu
    {
        private readonly IView.Profile.IClientRecords _view;
        private readonly ITask.Profile.IClientRecords _task;
        private DataTable _clientInstitutionData;


        public ClientRecords(IView.Profile.IClientRecords view) : this(view, new Task.Context.ClientRecords()) { }

        public ClientRecords(IView.Profile.IClientRecords view, ITask.Profile.IClientRecords task)
        {
            _view = view;
            _task = task;
        }

        public override void Initialize()
        {
            EnableTransactions(false);

            _task.OnGetClientInstitutionRecordByClientId += TaskOnGetClientInstitutionRecordByClientId;
            GetClientInstitutionRecord();
        }

        public void GetClientInstitutionRecord()
        {
            _task.GetClientInstitutionRecordByClientId(Convert.ToUInt64(_view.ProfileData.Rows[0]["id"].ToString()));
        }

        private void EnableTransactions(bool enable)
        {
            _view.EnableRegularLoan = enable;
            _view.EnableBenefitsLoan = enable;
            _view.EnableInstitution = enable;
            _view.EnableNewPayment = enable;
        }

        void TaskOnGetClientInstitutionRecordByClientId(object source, FormEvent e)
        {
            _clientInstitutionData = (DataTable)e.Value;
            
            if (_clientInstitutionData.Rows.Count.Equals(0))
                return;

            var dr = _clientInstitutionData.Rows[0];
            _view.Branch = string.Format("{0}-{1}", dr["branch_name"], dr["branch_code"]);
            _view.Institution = string.Format("{0}-{1}", dr["institution_name"], dr["institution_code"]);
            EnableTransactions(true);
        }

        protected override void TaskOnInsert(object source, FormEvent e)
        {
            //_view.ButtonAddEnable(true);
            TriggerModify(new FormEvent(e.IsSuccess) { Exception = e.Exception, Value = e.Value });
        }

        protected override void TaskOnUpdate(object source, FormEvent e)
        {
            //_view.ButtonUpdateEnable(true);
            TriggerModify(new FormEvent(e.IsSuccess) {Exception = e.Exception, Value = e.Value});
        }


        protected override void SetData()
        {
            if(Data.Rows.Count.Equals(0))
                return;

            //_view.Surname = Data.Rows[0]["last_name"].ToString();
            //_view.Firstname = Data.Rows[0]["first_name"].ToString();
            //_view.Midname = Data.Rows[0]["middle_name"].ToString();
            //_view.Gender = Data.Rows[0]["gender"].ToString();
            //_view.Address = Data.Rows[0]["address"].ToString();
            //_view.Birthdate = Convert.ToDateTime(Data.Rows[0]["birth_date"].ToString());
            //_view.Email = Data.Rows[0]["email"].ToString();
            //_view.Mobile = Data.Rows[0]["mobile_num"].ToString();
            //_view.Tel = Data.Rows[0]["tel_num"].ToString();
        }

        public override void Add()
        {
            if (!Validate())
                return;

            //_view.ButtonAddEnable(false);
            var a = Data.NewRow();

            //a["last_name"] = _view.Surname;
            //a["first_name"] = _view.Firstname;
            //a["middle_name"] = _view.Midname;
            //a["gender"] = _view.Gender;
            //a["address"] = _view.Address;
            //a["birth_date"] = _view.Birthdate;
            //a["email"] = _view.Email;
            //a["mobile_num"] = _view.Mobile;
            //a["tel_num"] = _view.Tel;
            //a["user_create_id"] = Convert.ToUInt64(Config.User.Rows[0]["id"].ToString());
            //a["user_last_name"] = Config.User.Rows[0]["last_name"].ToString();
            //a["user_first_name"] = Config.User.Rows[0]["first_name"].ToString();
            //a["user_middle_name"] = Config.User.Rows[0]["middle_name"].ToString();
            //a["user_name"] = Config.User.Rows[0]["user_name"].ToString();

            Data.Rows.Add(a);
            _task.Insert(Data);
        }

        public override void Modify()
        {
            if(!Validate())
                return;

            //_view.ButtonUpdateEnable(false);
            //Data.Rows[0]["last_name"] = _view.Surname;
            //Data.Rows[0]["first_name"] = _view.Firstname;
            //Data.Rows[0]["middle_name"] = _view.Midname;
            //Data.Rows[0]["gender"] = _view.Gender;
            //Data.Rows[0]["address"] = _view.Address;
            //Data.Rows[0]["birth_date"] = _view.Birthdate;
            //Data.Rows[0]["email"] = _view.Email;
            //Data.Rows[0]["mobile_num"] = _view.Mobile;
            //Data.Rows[0]["tel_num"] = _view.Tel;
            Data.Rows[0]["user_create_id"] = Convert.ToUInt64(Config.User.Rows[0]["id"].ToString());
            Data.Rows[0]["user_last_name"] = Config.User.Rows[0]["last_name"].ToString();
            Data.Rows[0]["user_first_name"] = Config.User.Rows[0]["first_name"].ToString();
            Data.Rows[0]["user_middle_name"] = Config.User.Rows[0]["middle_name"].ToString();
            Data.Rows[0]["user_name"] = Config.User.Rows[0]["user_name"].ToString();
            _task.Update(Data);
        }

        protected override bool Validate()
        {
            //if (string.IsNullOrEmpty(_view.Surname.Trim()))
            //{
            //    TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty surname.") });
            //    _view.FocusSurname();
            //    return false;
            //}

            //if (string.IsNullOrEmpty(_view.Firstname.Trim()))
            //{
            //    TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty firstname.") });
            //    _view.FocusFirstname();
            //    return false;
            //}

            //if (string.IsNullOrEmpty(_view.Midname.Trim()))
            //{
            //    TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty midname.") });
            //    _view.FocusMidname();
            //    return false;
            //}

            //if (string.IsNullOrEmpty(_view.Address.Trim()))
            //{
            //    TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty address.") });
            //    _view.FocusAddress();
            //    return false;
            //}

            TriggerValidate(new FormEvent(true));
            return true;
        }

        public DataTable GetClientInstitutionData()
        {
            return _clientInstitutionData;
        }
    }
}
